<?php
require_once "config.php";

$u_id = 4;
$code = $_POST['code'];
$c_id = $_POST['c_id'];


if($code!=0) {
    $zan_see = $mysql->query("select * from dyspw_conment_like where u_id=$u_id and conment_id=$c_id");
    $see_rows = $zan_see->fetch_all(MYSQLI_ASSOC);
    if(!$see_rows){
        $sql = $mysql->query("insert into dyspw_conment_like (id,u_id,conment_id,state) values (null,'{$u_id}','{$c_id}',1)");
        if($sql){
            $web_up = $mysql->query("update dyspw_usercomment set zan=zan+1 where id=$c_id");
            $wweb_sql =  $mysql->query("select * from dyspw_usercomment where id={$c_id}");
            $wweb_rows = $wweb_sql->fetch_all(MYSQLI_ASSOC);
            echo json_encode(['code'=>1,'rows'=>$wweb_rows[0]['zan'],'c_id'=>$wweb_rows[0]['id']]);
        }else{
            print_r("添加失败");die;
        }
    }else{
        $upd_sql = $mysql->query("update dyspw_conment_like set state=1 where u_id={$u_id} and conment_id=$c_id");

        if($upd_sql){
            $web_up = $mysql->query("update dyspw_usercomment set zan=zan+1 where id=$c_id");
            $wweb_sql =  $mysql->query("select * from dyspw_usercomment where id={$c_id}");
            $wweb_rows = $wweb_sql->fetch_all(MYSQLI_ASSOC);
            echo json_encode(['code'=>1,'rows'=>$wweb_rows[0]['zan'],'c_id'=>$wweb_rows[0]['id']]);
        }else{
            print_r("修改失败");die;
        }
    }

}else{
    $zan_see = $mysql->query("select * from dyspw_conment_like where u_id={$u_id} and conment_id={$c_id}");

    $see_rows = $zan_see->fetch_all(MYSQLI_ASSOC);
    if($see_rows){
        $sql = $mysql->query("update dyspw_conment_like set state=0 where u_id={$u_id} and conment_id=$c_id");
        if($sql){
            $web_sql =  $mysql->query("select * from dyspw_usercomment where id={$c_id}");
            $web_rows = $web_sql->fetch_all(MYSQLI_ASSOC);
            if($web_rows[0]['zan']>0){

                $web_up = $mysql->query("update dyspw_usercomment set zan=zan-1 where id={$c_id}");
                $wweb_sql =  $mysql->query("select * from dyspw_usercomment where id={$c_id}");
                $wweb_rows = $wweb_sql->fetch_all(MYSQLI_ASSOC);
                echo json_encode(['code'=>1,'rows'=>$wweb_rows[0]['zan'],'c_id'=>$wweb_rows[0]['id']]);
            }else{
                print_r("点赞失败");die;
            }
        }
    }

}




?>
